<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8"/>
    <title>内容管理列表 Conte List</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, content-scalable=0"/>
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all"/>

    <script th:inline="javascript">
        var moduleCode = [[${module}]];
        var navId = [[${nav}]];
        var typeInt = [[${type}]];
    </script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-form layui-card">
                <div class="layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <select  name="layui-form-field" id="layui-form-field" class="form-control input-sm" style="width:auto;">
                                <option value="" >选择搜索条件</option>
                                <option value="navName" >栏目名称</option>
                                <option value="title"  >文章标题</option>
                                <option value="content" >文章内容</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <input class="layui-input" name="keyword" id="layui-form-keyword" autocomplete="off"/>
                        </div>
                        <button class="layui-btn" data-type="search">搜索</button>
                    </div>
                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="content-list-table" lay-filter="content-list-table"></table>

                    <script type="text/html" id="content-list-table-operate-bar">
                        {{# if(d.id > 0 ){ }}
                        <a th:if="${editAction}" class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
                        {{# }else{ }}
                        <a th:if="${addAction}" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="add"><i class="layui-icon">&#xe654;</i>添加</a>
                        {{# } }}
                    </script>

                    <script type="text/html" id="content-list-table-switch-state-tpl">
                        <input type="checkbox" name="state" lay-skin="switch" lay-filter="content-list-table-state"
                               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.state == 1 ? 'checked' : '' }}>
                    </script>

                    <blockquote class="layui-elem-quote">
                        <div style="margin-bottom: 10px;">
                            本功能只适用于选用单页模型的导航栏目，每个导航只能有一个页面；
                        </div>
                    </blockquote>
                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , table = layui.table;

        var fieldName = $('#layui-form-field');
        var keyword = $('#layui-form-keyword');

        var tableIns = table.render({
            elem: '#content-list-table'
            , url: '/api/content/list?type=1'
            , id: 'id'
            , cols: [[
                {field: 'id', width: 60, title: 'ID'}
                , {field: 'navName', width: 120, title: '导航栏目', sort: true,unresize: true}
                , {field: 'moduleName', width: 100, title: '模型',unresize: true}
                , {field: 'title', title: '标题',templet: function(d){
                    if( d.picture == null || d.picture.trim.length == 0){
                        return d.title;
                    }else {
                        return '<span class="layui-badge layui-bg-orange">图</span>' + d.title;
                    }
                }}
                , {field: 'publishTime', width: 160, title: '发布时间', sort: true}
                , {
                    field: 'state', width: 80, title: '状态', fixed: 'right', sort: true, templet: '#content-list-table-switch-state-tpl', unresize: true
                }
                , {field: 'visits', width: 80, title: '访问量', fixed: 'right', unresize: true}
                , {width: 80, align: 'center', fixed: 'right', toolbar: '#content-list-table-operate-bar', unresize: true}
            ]]
        });

        form.on("switch(content-list-table-state)",function(obj){
            //var json = JSON.parse(decodeURIComponent($(this).data('json')));
            layer.load();
            var stateInt = obj.elem.checked ? 1 : 0;
            $.ajax({
                url: '/api/content/state',
                method: 'post',
                data: {id : this.value , state : stateInt},
                dataType: 'JSON',
                success: function (res) {
                    layer.closeAll();
                    if (res.code == '0') {
                        layer.msg('数据修改完成!', {icon: 1,time: 2000});
                    } else {
                        layer.msg('ERROR:数据修改错误!', {icon: 2,time: 2000});
                    }
                },
                error: function (err) {
                    layer.closeAll();
                    layer.msg('数据修改错误', {icon: 2,time: 2000});
                }
            });
            //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);

            //json = table.clearCacheKey(json);
            //console.log(json); //当前行数据
        });

        //监听行工具事件
        table.on('tool(content-list-table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'add':
                    var index = layer.open({
                        type: 2
                        , title: '添加内容'
                        , content: '/web/content/add?type=1&module='+data.moduleCode+'&navId='+data.navId
                        , maxmin: false
                        , area: ['600px', '500px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-content-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/content/add',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    layer.full(index);
                    break;
                case 'edit':
                    var index = layer.open({
                        type: 2
                        , title: '修改内容'
                        , content: '/web/content/edit?id=' + data.id
                        , maxmin: false
                        , area: ['600px', '500px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-content-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/content/edit',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    layer.full(index);
                    break;
            }
        });

        var active = {
            search: function () {
                //执行重载
                tableIns.reload({
                    where: {
                        fieldName : fieldName.val(),
                        keyword : keyword.val()
                    }
                });
            }
        };
        $('.layui-form .layui-form-item .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>